/*
 * @Author: your name
 * @Date: 2021-10-05 19:32:04
 * @LastEditTime: 2021-10-16 21:34:14
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \src\cplaywidget.h
 */
#ifndef GLPLAYWIDGET_H
#define GLPLAYWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>
#include <QOpenGLTexture>
#include <QFile>


#include <QTimer>
#include "api/video/i420_buffer.h"

#define ATTRIB_VERTEX 3
#define ATTRIB_TEXTURE 4


class CPlayWidget:public QOpenGLWidget,protected QOpenGLFunctions
{
    Q_OBJECT
public slots:
    void OnPaintData(const rtc::scoped_refptr<webrtc::I420BufferInterface> &buffer);
    void OnCameraData(rtc::scoped_refptr<webrtc::I420BufferInterface> &);

public:
    typedef enum{
        TYPE_YUV420P,
        TYPE_RGB32,
        TYPE_I420,
        TYPE_UNSET,
    }IMG_TYPE;
    CPlayWidget(QWidget* parent,IMG_TYPE type);

    CPlayWidget(QWidget* parent);
    ~CPlayWidget();
    int SetDataType(IMG_TYPE);
    int StartRender();
    int StopRender();
    int OnCameraData(uint8_t *);
    int SetImgSize(uint32_t width,uint32_t );
    int RenderWidth();
    int RenderHeight();
protected:
    QTimer tm;
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;
private:
    IMG_TYPE mType;  //YUV420P
    GLuint textureUniformY; //
    GLuint textureUniformU; //
    GLuint textureUniformV; //
    GLuint textureUniformRGB; //


    GLuint textureUnifromRGB; //rgb32

    GLuint id_rgb;
    GLuint id_y;
    GLuint id_u;
    GLuint id_v;

    QOpenGLTexture* m_pTextureRGB;  //RGB

    QOpenGLTexture* m_pTextureY;  //
    QOpenGLTexture* m_pTextureU;  //
    QOpenGLTexture* m_pTextureV;  //
    QOpenGLShader *m_pVSHader;  //
    QOpenGLShader *m_pFSHader;  //
    QOpenGLShaderProgram *m_pShaderProgram; //
    int m_nVideoW; //
    int m_nVideoH; //
    unsigned char *m_pBufYuv420p;
    unsigned char* m_pBufRgb32;

    void initShaderYuv();
    void initShaderRgb();
    void initShaderI420();
    int loadYuvTexture();
    int loadRtcI420Texture();
    int loadRgbTexture();
    rtc::scoped_refptr<webrtc::I420BufferInterface> m_buffer;
    bool m_start_render;
};
#endif
